package com.nami.logic.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.nami.logic.cache.grpmember.GrpMemberCache;
import com.nami.logic.service.GrpMemberService;
import com.nami.store.api.service.RemoteGroupMemberService;
import com.nami.store.api.response.groupmember.GroupMemberDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;


@Slf4j
@Service
@RequiredArgsConstructor
public class GrpMembersServiceImpl implements GrpMemberService {

    private final RemoteGroupMemberService grpMemberService;


    @Override
    public List<GroupMemberDTO> getGrpMembersFromCache(String groupId) {
        List<GroupMemberDTO> grpMembers = GrpMemberCache.getGrpMembers(groupId);
        if (grpMembers == null) {
            List<GroupMemberDTO> groupMembers = grpMemberService.getGroupMembers(groupId);
            if (CollUtil.isEmpty(groupMembers)) {
                return null;
            }
            GrpMemberCache.add(groupId, groupMembers);
            return grpMembers;
        }

        return grpMembers;
    }
}
